﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using OneOfAll.Common.Models;
using OneOfAll.Common.Util;
using OneOfAll.UserControls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneOfAll.ViewModel
{
    public class PeopleViewModel : ViewModelBase
    {
        #region 字段

        #endregion

        #region 属性
        private ObservableCollection<PeopleInfoControl> peopleInfoControlList = new ObservableCollection<PeopleInfoControl>();
        public ObservableCollection<PeopleInfoControl> PeopleInfoControlList
        {
            get { return peopleInfoControlList; }
            set { peopleInfoControlList = value; RaisePropertyChanged(); }
        }

        private PeopleModel selectedPeople = null;
        public PeopleModel SelectedPeople
        {
            get { return selectedPeople; }
            set { selectedPeople = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 命令
        private RelayCommand<string> buttonClickCmd;
        public RelayCommand<string> ButtonClickCmd
        {
            get
            {
                if (buttonClickCmd == null)
                {
                    buttonClickCmd = new RelayCommand<string>((p) => ButtonClickFunc(p));
                }
                return buttonClickCmd;
            }
        }


        #endregion

        #region 方法
        private void ButtonClickFunc(string p)
        {
            switch (p)
            {
                case "X":
                    Messenger.Default.Send("PeopleView", Global.GlobalResource.CloseChildrenWindow);
                    break;
            }
        }

        private void ArchiveChanged(ArchiveModel info)
        {
            PeopleInfoControlList = new ObservableCollection<PeopleInfoControl>();
            foreach (var item in Global.GlobalResource.ArchiveInfo.PeopleList)
            {
                PeopleInfoControl control = new PeopleInfoControl() { Info = item };
                control.MouseLeftButtonDown += Control_MouseLeftButtonDown;
                PeopleInfoControlList.Add(control);
            }
            Global.GlobalResource.ArchiveInfo.PeopleList.CollectionChanged -= PeopleList_CollectionChanged;
            Global.GlobalResource.ArchiveInfo.PeopleList.CollectionChanged += PeopleList_CollectionChanged;
        }

        private void PeopleList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    foreach (PeopleModel item in e.NewItems)
                    {
                        PeopleInfoControl control = new PeopleInfoControl() { Info = item};
                        control.MouseLeftButtonDown += Control_MouseLeftButtonDown;
                        PeopleInfoControlList.Add(control);
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    foreach (PeopleModel item in e.OldItems)
                    {
                        PeopleInfoControl control = PeopleInfoControlList.FirstOrDefault(t => t.Info.Key == item.Key);
                        if (control == null) continue;
                        control.MouseLeftButtonDown -= Control_MouseLeftButtonDown;
                        PeopleInfoControlList.Remove(control);
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                    PeopleInfoControlList.ForEach(x => x.MouseLeftButtonDown -= Control_MouseLeftButtonDown);
                    PeopleInfoControlList.Clear();
                    break;
            }
        }

        private void Control_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            PeopleInfoControl control = sender as PeopleInfoControl;
            if (control == null) return;
            SelectedPeople = control.Info;
            PeopleInfoControlList.ForEach(x => x.IsSelected = false);
            control.IsSelected = true;
        }

        private void RegisterMessages()
        {
            Messenger.Default.Register<ArchiveModel>(this, Global.GlobalResource.ArchiveChanged, new Action<ArchiveModel>(info =>
            {
                ArchiveChanged(info);
            }));
        }


        #endregion

        public PeopleViewModel()
        {
            RegisterMessages();
        }


    }
}
